
import { _decorator, Component, Node, Texture2D, Event, EventTouch, UIOpacity, tween, Vec3, v3 } from 'cc';
import { maskLayer } from '../../../resources/prefab/maskLayer';
import { BaseObject } from '../modules/baseObject';
import { entityUtil } from '../modules/entityUtil';
import { poolManager } from '../modules/poolManager';
import { uiManager } from '../modules/uiManager';
const { ccclass, property } = _decorator;

@ccclass('UiForm')
/**
 * @author {lizhitao}
 * @description 窗体的基类，带有mask层，可以自动关闭
 */
export class UiForm extends BaseObject {

    @property
    public isPopEffect: boolean = false;

    @property
    public isMask: boolean = false;

    @property
    public maskOpacity: number = 1;

    public fullView: boolean = true;
    public formName: string = "";
    private maskName = "maskLayer";

    private _opacity: number = 0;
    private _UIOpacity: UIOpacity = null;

    start() {

    }

    onLoad() {
        if (this.isPopEffect && this.isMask) {
            this._UIOpacity = this.getComponent(UIOpacity) || this.addComponent(UIOpacity);
            this._opacity = this._UIOpacity.opacity;
        }
        if (this.isMask)
            this.addMask();
    }

    showEffect() {
        if (this.isPopEffect) {
            // if (this.isMask) {
            //     this._UIOpacity.opacity = 0;
            //     tween(this._UIOpacity).to(0.1, { opacity: this._opacity }).start();
            // } else {
            this.node.setScale(Vec3.ONE);
            tween(this.node)
                .to(0.1, { scale: v3(1.1, 1.1, 1.1) })
                .to(0.1, { scale: Vec3.ONE })
                .start();
            // }
        }
    }
    closeEffect(callback: Function) {
        if (this.isPopEffect) {
            // if (this.isMask) {
            //     tween(this._UIOpacity).to(0.1, { opacity: 0 }).call(callback).start();
            // } else {
            this.node.setScale(Vec3.ONE);
            tween(this.node)
                .to(0.1, { scale: v3(1.1, 1.1, 1.1) })
                .to(0.1, { scale: Vec3.ONE })
                .call(callback)
                .start();
            // }
        } else {
            callback();
        }
        // callback();
    }

    public addMask() {
        if (this.node.getChildByName(this.maskName)) {
            this.node.active = true;
        }
        else {
            entityUtil.showEntity(this.maskName, this.node, (m) => {
                // var maskCtl = m.getComponent(maskLayer);
                m.setSiblingIndex(0);
                //! 不起作用
                m.on(Node.EventType.TOUCH_START, this.onMaskMouseDown, this);
                m.on(Node.EventType.TOUCH_MOVE, this.onMaskMouseDown, this);
                m.on(Node.EventType.TOUCH_END, this.onMaskMouseDown, this);
            });

        }
    }


    public removeMask() {
        if (this.node.getChildByName(this.maskName)) {
            this.node.active = false;
            return;
        }
    }

    public onMaskMouseDown(e: EventTouch) {
        e.propagationStopped = true;
    }

    public hideForm(e?: EventTouch) {
        // consolse.log('hideForm', e)
        uiManager.hideUIFormByNode(this.node);
        // poolManager.putNode(this.node);
    }




    hideAnim(cb: Function) {
        cb();
    }
}
